package it.unipd.dei.es1011.nucularc0d3.defaultproject;

import it.unipd.dei.es1011.nucularc0d3.defaultproject.util.MainArrayAdapter;
import it.unipd.dei.es1011.nucularc0d3.defaultproject.util.MainListViewItem;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
/**
 * This is the main activity, started when user tap app icon.
 * This activity show an header with the Logo of the program, a list
 * of what the user can do, and a footer with the url to the website.
 * 
 * @author darkfrank
 *
 */
public class MainActivity extends Activity {	
	public static final String PREFS_NAME = "MyPrefsFile";

	private static final String[] DATA = {
		"New Session", "List Sessions", "New Quick Session",  "Settings"
	};

	private long availableMegs;

	protected Dialog onCreateDialog(int id) {
		/** Available memory under 2MByte, Abort Operation. */
		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		builder.setMessage("The internal memory is low.\nFree other internal memory to continue.")
		.setCancelable(true)
		.setIcon(getResources().getDrawable(R.drawable.alert_dialog_icon))
		.setTitle("Action canceled!")
		.setNeutralButton("OK", null);
		return builder.create();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.mainactivity);

		ListView lv = (ListView) findViewById(R.id.mainList);
		MainArrayAdapter lines = new MainArrayAdapter(this);

		// create my first List View Item, New Session, so i load the suitable icon
		MainListViewItem newSession = new MainListViewItem();
		newSession.mIcon = BitmapFactory.decodeResource(getResources(), R.drawable.new_session);
		newSession.description = DATA[0];
		lines.add(newSession);
		// create my second List View Item, List Sessions, so i load the suitable icon
		MainListViewItem listSession = new MainListViewItem();
		listSession.mIcon = BitmapFactory.decodeResource(getResources(), R.drawable.list_session);
		listSession.description = DATA[1];
		lines.add(listSession);
		// create my third List View Item, New Quick Session, so i load the suitable icon
		MainListViewItem newQuickSession = new MainListViewItem();
		newQuickSession.mIcon = BitmapFactory.decodeResource(getResources(), R.drawable.new_quick_session);
		newQuickSession.description = DATA[2];
		lines.add(newQuickSession);
		// create my fourth List View Item, Settings, so i load the suitable icon
		MainListViewItem settings = new MainListViewItem();
		settings.mIcon = BitmapFactory.decodeResource(getResources(), R.drawable.settings);
		settings.description = DATA[3];
		lines.add(settings);
		// Set the adapter that creates the list.
		lv.setAdapter(lines);

		/** get the available internal free memory (MByte) */
		MemoryInfo mi = new MemoryInfo();
		ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		activityManager.getMemoryInfo(mi);
		availableMegs = mi.availMem / 1048576L;

		// Attach the On Click Listener to listen user iteration.
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {

				switch(position){
				case 0: // Start a new session
					if(availableMegs < 2){
						showDialog(0);
					}
					else{
						startActivity(new Intent(arg1.getContext(), NewSession.class));
					}
					break;
				case 1: // go to the list of sessions
					startActivity(new Intent(arg1.getContext(), ListSessionsActivity.class));
					break;
				case 2: // go directly to the recording activity, starting it
					if(availableMegs < 2){
						showDialog(0);
					}
					else{
						startActivity(new Intent(arg1.getContext(), Recording.class));
					}
					break;
				case 3: // go to the settings activity
					startActivity(new Intent(arg1.getContext(), Settings.class));
					break;
				}	
			}
		});
	}

	@Override
	protected void onResume() {		
		super.onResume();
		ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
			if ("it.unipd.dei.es1011.nucularc0d3.defaultproject.RecordingService".equals(service.service.getClassName())) {
				startActivity(new Intent(this, Recording.class));
			}
		}
	}
	
	
	
}